﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OCRUI.OCRBatchManager
{
    public class Line : BoundedTextBase<Word>, IBoundedText
    {
        public List<Word> Words  
        { 
            get 
            {
                return base.ChildItems;
            } 
            set 
            { 
                this.ChildItems = value; 
            } 
        }
        public new void Parse(List<string> document)
        {
            this.ChildItems = new List<Word>();
            List<int[]> wordRanges = FindWords(document);
            foreach (int[] range in wordRanges)
            {
                Word w = new Word();
                w.Parse(document, range);
                this.ChildItems.Add(w);

            }
            
        }

        public List<int[]> FindWords(List<string> document)
        {
            List<int[]> ranges = new List<int[]>();
            int wStart = this.BoundTextStartPos;
            int wEnd = int.MinValue;
            bool lastWasSpace = false;
            for (int i = wStart + 1; i <= wEnd; i++)
            {
                if (document[i].Trim().Length > 0 || !document[i].Trim().Contains("<nl>"))
                {
                    lastWasSpace = false;
                }
                else if(!lastWasSpace)
                {
                    int[] range = new int[2] { wStart, i-1 };
                    ranges.Add(range);
                    wStart = i =  i ++;
                    
                    lastWasSpace = true;
                }
            }
            return ranges;
        }

        
    }
}
